package com.sxt.opd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sxt.opd.domain.Guahao;
import com.sxt.opd.domain.Patient;
import com.sxt.opd.service.GuahaoService;
import com.sxt.opd.service.PatientService;
import com.sxt.opd.vo.GuahaoVo;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.domain.Dept;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.DeptService;
import com.sxt.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-01-08
 */
@RestController
@RequestMapping("/p/guahao")
public class GuahaoController {

    @Autowired
    private GuahaoService guahaoService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    /**
     * 查询
     */
    @HystrixCommand
    @RequestMapping("loadAllGuahao")
    public DataGridView loadAllGuahao(GuahaoVo guahaoVo) {
        IPage<Guahao> page = new Page<>(guahaoVo.getPage(), guahaoVo.getLimit());
        QueryWrapper<Guahao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(guahaoVo.getId()!=null&&guahaoVo.getId()!=0,"id",guahaoVo.getId());
        queryWrapper.eq(guahaoVo.getPatientid()!=null&&guahaoVo.getPatientid()!=0,"patientid",guahaoVo.getPatientid());
        queryWrapper.ge(guahaoVo.getStartTime()!=null, "createtime", guahaoVo.getStartTime());
        queryWrapper.le(guahaoVo.getEndTime()!=null, "createtime", guahaoVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(guahaoVo.getOperateperson()), "operateperson", guahaoVo.getOperateperson());
        queryWrapper.orderByAsc("createtime");
        this.guahaoService.page(page, queryWrapper);
        List<Guahao> records = page.getRecords();
        for (Guahao record : records) {
            Patient patient = this.patientService.getById(record.getPatientid());
            record.setPatient(patient);
            User user = userService.getById(record.getUserid());
            record.setDoctorname(user.getName());
            Dept dept = this.deptService.getById(user.getDeptid());
            record.setMenzhentitle(dept.getTitle());
            record.setDeptid(user.getDeptid());
        }
        return new DataGridView(page.getTotal(), records);
    }
    /**
     * 添加
     */
    @HystrixCommand
    @RequestMapping("addGuahao")
    public ResultObj addGuahao(GuahaoVo guahaoVo,String userName) {
        try {
            guahaoVo.setCreatetime(new Date());
            guahaoVo.setOperateperson(userName);
            this.guahaoService.save(guahaoVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改
     */
    @HystrixCommand
    @RequestMapping("updateGuahao")
    public ResultObj updateGuahao(GuahaoVo guahaoVo) {
        try {
            this.guahaoService.updateById(guahaoVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    /**
     * 删除
     */
    @HystrixCommand
    @RequestMapping("deleteGuahao")
    public ResultObj deleteGuahao(Integer id){
        try {
            this.guahaoService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

